package com.wondertek.mist;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.provider.Settings.Secure;

import com.eternity.classes.ATRP;
import com.eternity.classes.ATRP.Callback;

public class Utils {

	public static final String ACTION_NOTI = "com.wondertek.mist.action.NOTI";
	public static final String ACTION_MAM = "com.wondertek.mist.action.MAM";

	public static final int MSG_TO_MIST = 0x0001;



	;


	// 获取AppKey
	public static String getMetaValue(Context context, String metaKey) {
		Bundle metaData = null;
		String apiKey = null;
		if (context == null || metaKey == null) {
			return null;
		}
		try {
			ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
					context.getPackageName(), PackageManager.GET_META_DATA);
			if (null != ai) {
				metaData = ai.metaData;
			}
			if (null != metaData) {
				apiKey = metaData.getString(metaKey);
			}
		} catch (NameNotFoundException e) {

		}
		return apiKey;
	}

	public static void saveString(Context context, String param, String value) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				context.getPackageName(), Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString(param, value);
		editor.commit();
	}


	public static String getString(Context context, String param) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				context.getPackageName(), Activity.MODE_PRIVATE);
		String value = sharedPreferences.getString(param, "");
		return value;
	}

	public static void reset(Context context){
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				context.getPackageName(), Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.clear();
	}

	public static  void sendMamLog(Context ctx,String status){
		sendMamLog(ctx,status,new Callback() {
                    
        			public void onStart() {        				
        			}

        			public void onFailure(String msg) {      				
        			}

        			@Override
        			public void onSuccess(JSONObject result, Header[] headers) {        				
        			}
        		
        		});
	}
	
	public static  void sendMamLog(Context ctx,String status,Callback cb){

		/*
		String logUrl = Utils.getMetaValue(ctx, "logUrl");


		//状态
		try {
			String params = Utils.getString(ctx, "param");
			//新建一个新的jsonObject
			JSONObject js = new JSONObject();
			//从params中解析数据
			JSONObject jsonObject = new JSONObject(params);
			//工号
			js.put("sxUserId", jsonObject.getString("sxUserId"));
			//sessionToken
			js.put("sessionToken", jsonObject.getString("sessionToken"));
			//日期
			SimpleDateFormat  sDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");       
			String date = sDateFormat.format(new Date());
			js.put("date", date);
			//状态
			js.put("status", status);

			//设备号
			String deviceId = Secure.getString(ctx.getContentResolver(),Secure.ANDROID_ID);
			js.put("deviceId", deviceId);
			
			//包名
			js.put("packageName", ctx.getPackageName());

			//版本
			try {
				PackageManager manager = ctx.getPackageManager();
				PackageInfo info = manager.getPackageInfo(ctx.getPackageName(), 0);
				String version = info.versionName;
				js.put("versionName", version);
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			ATRP.request(logUrl, null, js, cb);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

   */
	}

}
